/**
 * Notes: 预约模块控制器 - 宠物门店预约小程序
 * Date: 2025-01-27 
 */

const BaseController = require('./base_controller.js');
const AppointmentService = require('../service/appointment_service.js');
const UserModel = require('../model/user_model.js');
const PassportService = require('../service/passport_service.js');

class AppointmentController extends BaseController {

	/**
	 * 创建预约单
	 */
	async createAppointment() {
		this.ensureAuthenticatedForWrite();
		// 数据校验
		let rules = {
			storeId: 'must|string|min:1|max:50|name=门店ID',
			serviceId: 'must|string|min:1|max:50|name=服务ID',
			petIds: 'must|array|name=宠物ID列表',
			appointmentTime: 'must|int|min:1|name=预约时间',
			employeeId: 'string|max:50|name=员工ID',
			deviceId: 'string|max:50|name=设备ID',
			couponId: 'string|max:50|name=优惠券ID',
			contactName: 'string|max:20|name=联系人姓名',
			contactPhone: 'string|max:20|name=联系人电话',
			remarks: 'string|max:200|name=备注信息',
			totalAmount: 'int|min:0|name=总金额',
			totalDays: 'int|min:1|name=寄养天数',
		};

		let input = this.validateData(rules);

		// 组合方案 2：下单前用户存在性校验与必要时自动建档
		let user = await UserModel.getOne({ _id: this._userId });
		if (!user) {
			const passport = new PassportService();
			await passport.registerUser(this._userId, {});
		}

		let service = new AppointmentService();
		return await service.createAppointment(this._userId, input);
	}

	/**
	 * 获取我的预约列表
	 */
	async getMyAppointments() {
		// 数据校验
		let rules = {
			status: 'int|in:101,201,301,302,401|name=状态筛选',
			page: 'int|min:1|name=页码',
			size: 'int|min:1|max:50|name=每页数量',
		};

		// 处理status参数：如果是false、true、null或其他非数字值，则设为undefined
		if (this._request.status === false || 
		    this._request.status === true ||
		    this._request.status === null || 
		    (typeof this._request.status !== 'number' && this._request.status !== undefined)) {
			this._request.status = undefined;
		}

		let input = this.validateData(rules);

		let service = new AppointmentService();
		return await service.getUserAppointments(
			this._userId, 
			input.status || null, 
			input.page || 1, 
			input.size || 20
		);
	}

	/**
	 * 获取预约详情
	 */
	async getAppointmentDetail() {
		// 数据校验
		let rules = {
			appointmentId: 'must|string|min:1|max:50|name=预约ID',
		};

		let input = this.validateData(rules);

		let service = new AppointmentService();
		return await service.getAppointmentDetail(input.appointmentId, this._userId);
	}

	/**
	 * 更新预约状态
	 */
	async updateAppointmentStatus() {
		this.ensureAuthenticatedForWrite();
		// 数据校验
		let rules = {
			appointmentId: 'must|string|min:1|max:50|name=预约ID',
			status: 'must|int|in:101,201,301,302,401|name=新状态',
		};

		let input = this.validateData(rules);

		let service = new AppointmentService();
		return await service.updateAppointmentStatus(input.appointmentId, input.status, this._userId);
	}

	/**
	 * 取消预约
	 */
	async cancelAppointment() {
		this.ensureAuthenticatedForWrite();
		// 数据校验
		let rules = {
			appointmentId: 'must|string|min:1|max:50|name=预约ID',
		};

		let input = this.validateData(rules);

		let service = new AppointmentService();
		return await service.cancelAppointment(input.appointmentId, this._userId);
	}

	/**
	 * 删除预约（软删除）
	 */
	async deleteAppointment() {
		this.ensureAuthenticatedForWrite();
		// 数据校验
		let rules = {
			appointmentId: 'must|string|min:1|max:50|name=预约ID',
		};

		let input = this.validateData(rules);

		let service = new AppointmentService();
		return await service.deleteAppointment(input.appointmentId, this._userId);
	}
}

module.exports = AppointmentController;
